﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for NguoiDungManager
/// </summary>
public class NguoiDungManager
{
    NguoiDungDAO nguoiDungDao;
	public NguoiDungManager()
	{
        nguoiDungDao = new NguoiDungDAO();
	}

    public DataTable AdminSearchNguoiDung(NguoiDung nguoiDung, DateTime? dateEnd)
    {
        DataTable dt = nguoiDungDao.GetNguoiDungSearch(nguoiDung, dateEnd);
        if (dt == null)
            return new DataTable();
        return dt;
    }

    /// <summary>
    /// Kiểm tra email đã được sử dụng hay chưa
    /// </summary>
    /// <param name="email"></param>
    /// <returns>True: đã sử dụng</returns>
    public bool CheckEmailUsed(string email)
    {
        DataTable dt = nguoiDungDao.GetNguoiDungByEmail(email);
        if (null == dt || dt.Rows.Count == 0)
            return false;

        return true;
    }

    public bool CheckUsernameUsed(string username)
    {
        DataTable dt = nguoiDungDao.GetNguoiDungByUsername(username);
        if (null == dt || dt.Rows.Count == 0)
            return false;

        return true;
    }

    public bool CheckLogin(NguoiDung nguoiDung)
    {
        DataTable dt = nguoiDungDao.GetNguoiDungByUsername(nguoiDung.Username);
        if (null == dt || dt.Rows.Count == 0)
            return false;

        if (!CommonFuns.HashSHA1(nguoiDung.Username + nguoiDung.MatKhau).Equals(dt.Rows[0]["MatKhau"]))
            return false;

        nguoiDung.Fullname = Convert.ToString(dt.Rows[0]["HoTen"]);
        nguoiDung.Email = Convert.ToString(dt.Rows[0]["Email"]);
        nguoiDung.DienThoai = Convert.ToString(dt.Rows[0]["DienThoai"]);
        nguoiDung.MatKhau = "";
        return true;
    }

    public bool InsertNguoiDung(NguoiDung nguoiDung)
    {
        return nguoiDungDao.InsertNguoiDung(nguoiDung);
    }

    public bool DeleteNguoiDung(NguoiDung nguoiDung)
    {
        return nguoiDungDao.DeleteNguoiDung(nguoiDung);
    }

    public bool UpdateNguoiDungAdmin(NguoiDung nguoiDung)
    {
        return nguoiDungDao.UpdateNguoiDungAdmin(nguoiDung);
    }
}